﻿using System;
using System.Collections.Generic;
using System.Linq;
using IQToolkit;
using IQToolkit.Data;
using Infrastucture.Factories;
using TimeSplicerModule.DataModels;
using TimeSplicerModule.DomainModels;

namespace TimeSplicerModule.Factories
{
    public interface IApplicationRolePermissionFactory : Infrastucture.Factories.IFactory<ApplicationRolePermissionDataModel, ApplicationRolePermissionModel>
    {
        ApplicationRolePermissionModel CreateNew(Guid applicationRoleGuid, Guid applicationItemGuid, Guid applicationPermissionGuid, ApplicationRoleModel role, ApplicationItemModel item, ApplicationPermissionModel permission);
        List<ApplicationRolePermissionModel> GetList(List<ApplicationRoleModel> roles, List<ApplicationItemModel> items, List<ApplicationPermissionModel> permissions);
        List<ApplicationRolePermissionModel> GetList(Func<ApplicationRolePermissionDataModel, bool> where, List<ApplicationRoleModel> roles, List<ApplicationItemModel> items, List<ApplicationPermissionModel> permissions);
    }

    public class ApplicationRolePermissionFactory : Factory<ApplicationRolePermissionDataModel, ApplicationRolePermissionModel>, IApplicationRolePermissionFactory
    {
        public ApplicationRolePermissionFactory(DbEntityProvider provider, IEntityTable<ApplicationRolePermissionDataModel> db)
            : base(provider, db)
        {

        }

        public ApplicationRolePermissionModel CreateNew(Guid applicationRoleGuid, Guid applicationItemGuid, Guid applicationPermissionGuid, ApplicationRoleModel role, ApplicationItemModel item, ApplicationPermissionModel permission)
        {
            return new ApplicationRolePermissionModel
            {
                ApplicationRoleGuid = applicationRoleGuid,
                ApplicationItemGuid = applicationItemGuid,
                ApplicationPermissionGuid = applicationPermissionGuid,
                Role = role,
                Item = item,
                Permission = permission
            };
        }

        public List<ApplicationRolePermissionModel> GetList(List<ApplicationRoleModel> roles, List<ApplicationItemModel> items, List<ApplicationPermissionModel> permissions)
        {
            var recs = this.GetList();

            foreach (var r in recs)
            {
                r.Role = roles.FirstOrDefault(x => x.ApplicationRoleGuid == r.ApplicationRoleGuid);
                r.Item = items.FirstOrDefault(x => x.ApplicationItemGuid == r.ApplicationItemGuid);
                r.Permission = permissions.FirstOrDefault(x => x.ApplicationPermissionGuid == r.ApplicationPermissionGuid);
            }

            return recs;
        }


        public List<ApplicationRolePermissionModel> GetList(Func<ApplicationRolePermissionDataModel, bool> where, List<ApplicationRoleModel> roles, List<ApplicationItemModel> items, List<ApplicationPermissionModel> permissions)
        {
            var recs = this.GetList(where);
      

            foreach (var r in recs)
            {
                r.Role = roles.FirstOrDefault(x => x.ApplicationRoleGuid == r.ApplicationRoleGuid);
                r.Item = items.FirstOrDefault(x => x.ApplicationItemGuid == r.ApplicationItemGuid);
                r.Permission = permissions.FirstOrDefault(x => x.ApplicationPermissionGuid == r.ApplicationPermissionGuid);
            }

            return recs;
        }
    }
}
